<?php
header("Content-type:text/html;charset=utf-8");
require_once '../source/core/run.php';
require CHENCY_ROOT.'./source/conf/city.php';
global $db, $tpl, $config;

//查询所有tags
$field = "SHOW FULL FIELDS FROM ".DB_PREFIX."tag";
$resul_fiedl = $db->getall($field);
$feilds  = array();
$tag_sql = '';
foreach ($resul_fiedl as $key => $fiel) {
	$feilds[] = $fiel['Field'];
}
if(in_array("enabled", $feilds)){
	$tag_sql="SELECT `tag` FROM ".DB_PREFIX."tag WHERE enabled=1 ORDER BY `tagid` DESC";
}else{
	$tag_sql="SELECT `tag` FROM ".DB_PREFIX."tag ORDER BY `tagid` DESC";
}
$tag_result=$db->getall($tag_sql);
$tags='';
foreach ($tag_result as $key => $val) {
	$tags.=$val['tag'].',';
}
$keyword=rtrim($tags, ',');
$keyarray=explode(',', $keyword);
//查找所有区域名称
$sql = "SELECT `name` FROM ".DB_PREFIX."region";
$region = $db->getall($sql);
$reg='';
//把tags里面的区域去重，并让后台区域与tags匹配
if(!empty($region))
{
	foreach ($keyarray as $key => $value) 
	{
		foreach ($region as $id => $cityn)
		{
			if(($pos = strpos($value,$cityn['name'])) !== false)
			{
				$keyarray[$key] = str_replace($cityn['name'], '', $value);
				break;
			}
		}
	}
	foreach (array_unique($keyarray) as $key => $vals) 
	{
		foreach ($region as $keys => $regs) 
		{
			$reg.=$regs['name'].$vals.',';
		}
	}
}
$keyarray = array_unique($keyarray);
//把tags中开启了关键词的匹配后置关键词
$postwords = $keyarray;
$post_word = $config['tailword'];
$postarray = explode(',', $post_word);
$posts = '';
foreach ($postwords as $key => $val) 
{
	foreach ($postarray as $post => $postword) 
	{
		if(($postsd = strpos($val, $postword)) !== false)
		{
			$postwords[$key] = str_replace($postword, '', $val);
			break;
		}
	}

}
foreach (array_unique($postwords) as $key => $value) 
{
		foreach ($postarray as $pos => $valpos) 
		{
			$posts.= $value.$valpos.',';
		}
}
//匹配区域与tags匹配
// $matsql  = "SELECT `name` FROM ".DB_PREFIX."matchregion where flag=1";
// $subtion = $db->getall($matsql);
$str='';
$strold='';
// if(!empty($subtion))
// {
// 	foreach ($subtion as $key => $val) 
// 	{
// 		foreach($keyarray as $word => $cit)
// 		{
// 			$strold.= $val['name'].$cit.',';
// 		}
// 	}
// }
$strold    = rtrim($tags.$posts.$reg, ',');
$str_array = array_unique(explode(',', $strold));
$keywords  = $db->getall("SELECT * FROM ".DB_PREFIX."keywords where flag=1 ORDER BY id DESC");
if($config['w_automatic'] == 1){
	foreach ($str_array as $key => $value) {
		$str.=$value.',';
	}
	foreach ($keywords as $ke_s => $words) {
		$str.=$words['wname'].',';
	}
}elseif ($config['w_automatic'] == 2) {
	foreach ($tag_result as $key => $value) {
		$str.=$value['tag'].',';
	}
	foreach ($keywords as $ke_s => $words) {
		$str.=$words['wname'].',';
	}
}
// if($config['w_automatic'] == 1){
// 	foreach ($str_array as $key => $value) {
// 		$str.=$value.',';
// 	}
// }
// if($config['w_manual'] == 1){
// 	foreach ($keywords as $ke_s => $words) {
// 		$str.=$words['wname'].',';
// 	}
// }
$str = rtrim($str, ',');
// $lengths=count(explode(',', $str));
// echo $lengths.'<br/>';

